import { Controller, Inject, Get, Query, Body, Post, Put, Param, Del } from '@midwayjs/core';
import { RoleService } from "@service/system/role.service";
import { ListRoleDTO, CreateRoleDTO, UpdateRoleDTO, ListAuthUserDTO, CancelAuthUserDTO, BatchCancelAuthUserDTO, BatchBindAuthUserDTO } from "@dto/system/role.dto";
import { Auth } from "@decorator/auth.decorator";
import { BusinessType, Log } from "@decorator/log.decorator";

@Controller('/system/role')
export class PostController {
  @Inject()
  roleService: RoleService;

  // 获取列表
  @Auth('system:role:list')
  @Get('/list')
  async list(@Query() queryParams: ListRoleDTO) {
    return await this.roleService.list(queryParams);
  }

  // 新增
  @Auth('system:role:add')
  @Log({ title: '角色管理', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() role: CreateRoleDTO) {
    return await this.roleService.create(role);
  }

  // 删除
  @Auth('system:role:remove')
  @Log({ title: '角色管理', businessType: BusinessType.REMOVE })
  @Del('/:roleId')
  async delete(@Param('roleId') roleId: string) {
    return await this.roleService.delete(roleId);
  }

  // 修改
  @Auth('system:role:edit')
  @Log({ title: '角色管理', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() role: UpdateRoleDTO) {
    return await this.roleService.update(role);
  }

  // 获取详情
  @Auth('system:role:query')
  @Get('/:roleId')
  async get(@Param('roleId') roleId: number) {
    return await this.roleService.detail(roleId);
  }

  // 已分配角色的用户列表
  @Get('/authUser/allocatedList')
  async allocatedList(@Query() queryParams: ListAuthUserDTO) {
    return await this.roleService.allocatedUserList(queryParams);
  }

  // 未分配角色的用户列表
  @Get('/authUser/unallocatedList')
  async unallocatedList(@Query() queryParams: ListAuthUserDTO) {
    return await this.roleService.unallocatedUserList(queryParams);
  }

  // 解绑角色用户
  @Auth('system:role:edit')
  @Put('/authUser/cancel')
  async cancelAuthUser(@Body() params: CancelAuthUserDTO) {
    return await this.roleService.cancelAuthUser(params);
  }

  // 批量解绑角色用户
  @Auth('system:role:edit')
  @Put('/authUser/cancelAll')
  async batchCancelAuthUser(@Body() params: BatchCancelAuthUserDTO) {
    return await this.roleService.batchCancelAuthUser(params);
  }

  // 批量绑定角色用户
  @Auth('system:role:edit')
  @Put('/authUser/allocateAll')
  async batchBindAuthUser(@Body() params: BatchBindAuthUserDTO) {
    return await this.roleService.batchBindAuthUser(params);
  }
}
